const ajax = require('../../tool/ajax');

/**
 * 案件详情右侧按钮脚本
 * @param pupConfig 框架基础配置信息
 * @param caseId 案件ID
 * @param index 需要点击按钮的索引,默认为0.
 * @param callback 获取案件详情接口后的回调
 * @returns {Promise<boolean>} 是否执行下一步
 */
const start = async (pupConfig, caseId, index = 0, callback) => {
  //是否执行下一步
  let goNext = true;
  //案件详情url拼接'&#=$'，是为了避免打开相同案件详情时，页面不刷新，从而导致ajax.waitForResponse()异常
  await pupConfig.page.goto(pupConfig.config.common.host + 'caseDetail?caseId=' + caseId + '&#=$');
  let res = await ajax.waitForResponse(pupConfig.page, 'getAllCaseInfo.do');
  //执行回调
  if (callback) {
    goNext = await callback(res);
  }

  if (goNext) {
    await pupConfig.page.waitFor('.zc-detail-top-right button');
    let buttons = await pupConfig.page.$$('.zc-detail-top-right button');
    await buttons[index].click();
  }
  return goNext
}

module.exports = start;